import winsound
import datetime
import os
import schedule
import threading
import time
from send_message import send_message

host_list = {}
mutex = threading.Lock()


def ping_ip(ip: str, host_name: str):
    # global host_list
    if os.system('ping -n 1 -w 800 %s' % ip):
        # del host_list[ip]
        # while os.system('ping -n 1 -w 2000 %s' % ip):
        print(datetime.datetime.now(), host_name + ',IP地址:' + ip + ',测试不通!')
        send_message(str(datetime.datetime.now()) + '  ' + host_name + ',IP地址:' + ip + ',测试不通!')
            # winsound.Beep(800, 2000)  # 其中600表示声音大小，1000表示发生时长，1000为1秒
            # time.sleep(10 * 60)
        # else:
            # host_list.update({ip: host_name})
    else:
        print(datetime.datetime.now(), host_name + ',IP地址:' + ip + ',通信正常!')


def ping_test():
    for key, value in host_list.items():
        threading.Thread(target=ping_ip, args=(key, value)).start()
    print('-' * 30)


def self_test():
    send_message("我是IP检测程序,老板,我在坚守岗位,请放心")


if __name__ == '__main__':
    host_list.update({'172.18.0.1': '总公司主服'})
    # host_list.update({'10.10.10.10': '测试IP'})
    host_list.update({'172.18.23.2': '备服'})
    host_list.update({'172.18.23.4': '知而行服务器'})
    host_list.update({'195.3.23.244': 'UPS主机'})
    host_list.update({'172.18.23.253': '打印服务器'})
    host_list.update({'172.18.23.250': '代理服务器'})
    host_list.update({'portal.idc1.fn': '淘先达'})
    host_list.update({'10.201.128.1': '淘先达DNS'})
    host_list.update({'st23api-rt03.inner.rt-mart.com': 'API服务器'})
    host_list.update({'vpnct.feiniu.com': '飞牛IDC'})
    host_list.update({'10.97.97.254': '汇聚交换机'})
    host_list.update({'10.97.96.1': '华为防火墙'})
    host_list.update({'10.97.96.5': '老防火墙'})
    host_list.update({'10.97.97.1': '中控'})
    host_list.update({'10.97.97.2': '蔬果'})
    host_list.update({'10.97.97.3': '服饰'})
    host_list.update({'10.97.97.4': '2F后仓'})
    host_list.update({'10.97.97.5': '仓管'})
    host_list.update({'10.97.97.6': '联合办公区'})
    ping_test()
    schedule.every().hour.do(ping_test)
    schedule.every().day.at("07:30").do(self_test)
    while True:
        schedule.run_pending()
        time.sleep(1)
